package com.jlnk.weatherdemo.view;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

import com.jlnk.weatherdemo.R;
import com.jlnk.weatherdemo.entity.WeatherInfo;
import com.jlnk.weatherdemo.presenter.WeatherPresenter;

public class WeatherActivity extends AppCompatActivity implements WeatherView {
    private WeatherPresenter presenter;
    private TextView cityNameTextView;
    private TextView currentDateTextView;
    private TextView currentTypeTextView;
    private TextView currentTemperatureTextView;
    private TextView currentWindTextView;
    private ListView futureWeatherListView;
    private ProgressBar progressBar;
    private EditText searchCityEditText;
    private Button changeCityButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather);

        cityNameTextView = findViewById(R.id.city_name);
        currentDateTextView = findViewById(R.id.current_date);
        currentTypeTextView = findViewById(R.id.current_type);
        currentTemperatureTextView = findViewById(R.id.current_temperature);
        currentWindTextView = findViewById(R.id.current_wind);
        futureWeatherListView = findViewById(R.id.future_weather_list);
        progressBar = findViewById(R.id.progress_bar);
        searchCityEditText = findViewById(R.id.search_city);
        changeCityButton = findViewById(R.id.change_city);

        presenter = new WeatherPresenter(this);
        presenter.loadWeatherInfo("北京市"); // 加载默认城市天气

        changeCityButton.setOnClickListener(v -> {
            String city = searchCityEditText.getText().toString();
            if (!city.isEmpty()) {
                presenter.loadWeatherInfo(city);
            }
        });
    }

    @Override
    public void showWeatherInfo(WeatherInfo weatherInfo) {
        // 更新当前天气信息
        cityNameTextView.setText(weatherInfo.getCity());
        currentDateTextView.setText(weatherInfo.getData().getDate());
        currentTypeTextView.setText(weatherInfo.getData().getType());
        currentTemperatureTextView.setText(weatherInfo.getData().getLow() + "°C - " + weatherInfo.getData().getHigh() + "°C");
        currentWindTextView.setText(weatherInfo.getData().getFengxiang() + " " + weatherInfo.getData().getFengli());
    }

    @Override
    public void showLoading() {
        progressBar.setVisibility(android.view.View.VISIBLE);
    }

    @Override
    public void hideLoading() {
        progressBar.setVisibility(android.view.View.GONE);
    }

    @Override
    public void showError(String message) {
        android.widget.Toast.makeText(this, message, android.widget.Toast.LENGTH_SHORT).show();
    }
}